package java_thinking.unit_18_IO._18_7;

import java.io.IOException;
import java.util.*;

/**
 * @Desc:   读取文件，并统计不同字符出现的次数
 * @author: cww
 * @DateTime: 2019-05-15 23:11
 */

public class E17_CharactersInfo {
    static String file = "src/maindemo/java/java_thinking/unit_18_IO/_18_7/E17_CharactersInfo.java";
    public static void main(String[] args) throws IOException {
        Map<Character,Integer> charStat = new HashMap<>();
        for (String s : new TextFile(file,"\\W+")) {
            for (int i = 0; i < s.length(); i++) {
                Character c = s.charAt(i);
                Integer num = charStat.get(c);
                charStat.put(c, num == null ? 1 : num + 1);
            }
        }
        //为什么这里要用new Character[0] 见https://blog.csdn.net/u013749540/article/details/72158887
        List<Character> keys = Arrays.asList(charStat.keySet().toArray(new Character[0]));
        Collections.sort(keys);
        for (Character c : keys) {
            System.out.println(c + "=> " + charStat.get(c));
        }
    }
}
